package com.iven.xie.entity;

import lombok.Data;

/**
 * mysql数据库表字段信息
 *
 * @author xiezhendon
 */
@Data
public class MysqlDbColumn extends DbTableColumn {
    // 所属库
    private String tableSchema;
    // 所属表
    private String tableName;
    // 列名
    private String columnName;
    // bigint(11)，varchar(30)
    private String columnType;
    // bigint->Long，int->Integer，
    // varchar/char->String，
    // timestamp->Timestamp，
    // date/time->Date，
    // float->Float，
    // double/decimal->Double，
    // blob->byte[]
    private String dataType;
    // 列缺省值
    private Object columnDefault;
    // 列最大字节长度，utf-8：
    // characterMaximumLength*
    // 3，gbk：characterMaximumLength* 2
    private Long characterOctetLength;
    // 列最大长度
    private Long characterMaximumLength;
    // 字节顺序码，从1开始
    private Integer ordinalPosition;
    // 是否可为空，YES：可空，NO：非空
    private String isNullable;
    // 列键值,PRI主键
    private String columnKey;
    // auto_increment 自增长
    private String extra;
    // 列描述
    private String columnComment;
    // 列权限集合，以逗号隔开
    private String privileges;

    public MysqlDbColumn setDataType(String dataType) {
        if (dataType != null) {
            this.dataType = dataType.toUpperCase();
            if (this.dataType.equals("INT")) {
                this.dataType = "INTEGER";
            } else if (this.dataType.equals("DATETIME")) {
                this.dataType = "TIMESTAMP";
            }
        }
        return this;
    }

    public boolean isPrimaryKey() {
        return columnKey != null && columnKey.equalsIgnoreCase("PRI");
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append(this.getClass().getName());
        sb.append(" # tableSchema=" + (tableSchema == null ? "null" : tableSchema.toString()));
        sb.append("; tableName=" + (tableName == null ? "null" : tableName.toString()));
        sb.append("; columnName=" + (columnName == null ? "null" : columnName.toString()));
        sb.append("; columnType=" + (columnType == null ? "null" : columnType.toString()));
        sb.append("; dataType=" + (dataType == null ? "null" : dataType.toString()));
        sb.append("; characterMaximumLength=" + (characterMaximumLength == null ? "null" : characterMaximumLength.toString()));
        sb.append("; characterOctetLength=" + (characterOctetLength == null ? "null" : characterOctetLength.toString()));
        sb.append("; columnDefault=" + (columnDefault == null ? "null" : columnDefault.toString()));
        sb.append("; ordinalPosition=" + (ordinalPosition == null ? "null" : ordinalPosition.toString()));
        sb.append("; isNullable=" + (isNullable == null ? "null" : isNullable.toString()));
        sb.append("; columnKey=" + (columnKey == null ? "null" : columnKey.toString()));
        sb.append("; extra=" + (extra == null ? "null" : extra.toString()));
        sb.append("; columnComment=" + (columnComment == null ? "null" : columnComment.toString()));
        sb.append("; privileges=" + (privileges == null ? "null" : privileges.toString()));
        return sb.toString();
    }

}
